<?php
/**
 * Larfree Api类
 * @author xiao
 */
namespace App\Http\Controllers\Api\Express;

use Illuminate\Http\Request;
use Larfree\Controllers\ApisController as Controller;
use App\Models\Express\ExpressProblem;
use Illuminate\Support\Facades\Cache;
class ProblemController extends Controller
{
    public function __construct(ExpressProblem $model)
    {
        $this->model = $model;
        parent::__construct();
    }

    public  function  index(Request $request)
    {
        $this->model=$this->model->orderBy('id','desc')->where("chose",'1');
        return parent::index($request); // TODO: Change the autogenerated stub
    }

    public function show($id, Request $request)
    {

        Cache::remember('news'.$id.$request->getClientIp(), 1, function ()  use ($id){
        $news=$this->model->where('id',$id)->first();
        $news->increment('read_number',1);
        $news->save();
        return $news;
       });

        $after = $this->model->select(['id','title'])->where('id','>',$id)->orderBy('id','asc')->first();
        $before  =$this->model->select(['id','title'])->where('id','<',$id)->orderBy('id','desc')->first();
        $data=[
            'content'=>$this->model->find($id),
            'before'=>$before,
            'after'=>$after,
        ];
        return $data;

    }

    public function store(Request $request)
    {

        $model = $this->model;
        $data = $request->all();
        $data['user_id']=getLoginUserID();
        $model->startLog();
        $data = $model->create($data);
        return Response()->success($data,'添加成功');
    }
}